package com.pf.controller;

import com.pf.common.dto.ResultResp;
import com.pf.common.utils.ResultRespUtils;
import com.pf.dto.AuditTaskDTO;
import com.pf.selection.constant.ConverterConstant;
import com.pf.selection.utils.SelectionEchoUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.util.Lists;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2023/4/2 11:23
 */
@RestController
@RequestMapping("/selection")
public class SelectionEchoController {

    @GetMapping("/echo")
    public ResultResp echo(@RequestParam(value = "nums", required = false) Integer nums) throws Exception {
        List<AuditTaskDTO> auditTaskList = getAuditTaskList(nums);
        Map<String, Map<String, String>> dataDictMap = getDataDict();
        long startTime = System.currentTimeMillis();
        SelectionEchoUtils selectionEchoUtils = new SelectionEchoUtils(AuditTaskDTO.class);
        Map selectionMap = selectionEchoUtils.getSelectionMap(auditTaskList, dataDictMap);
        long endTime = System.currentTimeMillis();
        System.out.println("消耗时间：" + (endTime - startTime) + "ms");
        return ResultRespUtils.success(selectionMap);
    }

    /**
     * 模拟查询业务数据
     *
     * @return
     */
    private List<AuditTaskDTO> getAuditTaskList() {
        AuditTaskDTO t1 = AuditTaskDTO.builder().id(1).taskName("abc").criteriaType("1").taskStatus("Open").remark("xcd").build();
        AuditTaskDTO t2 = AuditTaskDTO.builder().id(2).taskName("cds").criteriaType("2").taskStatus("Close").remark("aca").build();
        AuditTaskDTO t3 = AuditTaskDTO.builder().id(3).build();
        AuditTaskDTO t4 = AuditTaskDTO.builder().id(4).taskName("aax").criteriaType("2").taskStatus("Open").remark("bca").build();
        AuditTaskDTO t5 = AuditTaskDTO.builder().id(5).taskName("bas").criteriaType("1").taskStatus("Open").remark("abc").build();
        return Lists.newArrayList(t1, t2, t3, t4, t5);
    }

    /**
     * 模拟2000条业务数据
     *
     * @return
     */
    private List<AuditTaskDTO> getAuditTaskList(Integer nums) {
        if (nums == null) {
            return Lists.newArrayList();
        }
        List<AuditTaskDTO> taskList = Lists.newArrayList();
        AuditTaskDTO first = AuditTaskDTO.builder().id(1).build();
        taskList.add(first);
        for (int i = 1; i < nums; i++) {
            String taskName = RandomStringUtils.randomAlphanumeric(4);
            String criteriaType = i % 2 == 0 ? "1" : "2";
            String taskStatus = i % 2 == 0 ? "Open" : "Close";
            String remark = RandomStringUtils.randomAlphabetic(4);
            AuditTaskDTO task = AuditTaskDTO.builder().id(i + 1).taskName(taskName).criteriaType(criteriaType).taskStatus(taskStatus).remark(remark).build();
            taskList.add(task);
        }
        return taskList;
    }

    /**
     * 模拟查询数据字典数据
     *
     * @return
     */
    private Map<String, Map<String, String>> getDataDict() {
        Map<String, Map<String, String>> dataDictMap = new HashMap<>();
        Map<String, String> criteriaTypeMap = new HashMap<>();
        criteriaTypeMap.put("1", "智选");
        criteriaTypeMap.put("2", "车规");
        dataDictMap.put(ConverterConstant.CRITERIA_TYPE, criteriaTypeMap);
        return dataDictMap;
    }
}
